package com.yzs.controller.front;

import com.yzs.common.bean.R;
import com.yzs.entity.ForgetPwd;
import com.yzs.entity.User;
import com.yzs.service.IUserService;
import com.yzs.utils.JwtUtil;
import com.yzs.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService us;
    @Autowired
    JwtUtil JwtUtil;

    @RequestMapping("/loginUser")
    public R loginUser(@RequestBody User user){
        return us.loginUser(user);
    }

    @RequestMapping("/registerUser")
    public R registerUser(@RequestBody User user){
        return us.registerUser(user);
    }

    @RequestMapping("/loginOutUser")
    public R loginOutUser(HttpServletRequest req){
        boolean isRemove = JwtUtil.removeUserToken(req.getHeader("token"));
        if (!isRemove){
            return R.error("用户状态过期，请重新登录！");
        }
        return R.ok("用户已退出！");
    }

    @RequestMapping("/getSalerById")
    public R getSalerById(@RequestParam Integer id){
        return us.getSalerById(id);
    }

    @RequestMapping("/changePwd")
    public R changePwd(@RequestParam String oldPwd,@RequestParam String newPwd,HttpServletRequest req){
        String token = req.getHeader("token");
        return us.changePwd(oldPwd,newPwd,token);
    }

    @RequestMapping("getVerifyCode")
    public R getVerifyCode(@RequestParam String name){
        return us.getVerifyCode(name);
    }

    @RequestMapping("forgetPwd")
    public R forgetPwd(@RequestBody ForgetPwd forgetPwd){
        return us.forgetPwd(forgetPwd);
    }
}
